home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3241 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.4 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c,gnu.gcc.help
  4. Subject: Re: Casting unsigned short as unsigned int -> Bus error
  5. Date: Sat, 27 Jan 96 00:11:57 GMT
  6. Organization: none
  7. Message-ID: <822701517snz@genesis.demon.co.uk>
  8. References: <simmons.820857453@rzdspc1> <820879186snz@genesis.demon.co.uk> <4e8v6t$bd2@crl.crl.com>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4e8v6t$bd2@crl.crl.com> gherlein@crl.com "Greg Herlein" writes:
  15.  
  16. >This sounds to me like a problem I had a while back - though I'm not a
  17. >Sparc guru enough to know for sure.  I was reading elements from
  18. >structures like this too, and got bus errors for not reading on 32 bit
  19. >boundaries.  Does it work if you read the myshort variable into a
  20. >short *then* cast it to an unsigned int?  I'm not sure what the order
  21. >of operations is on the assignment... I would think the dererencing of
  22. >the stucture element poinet would be first, but what do I know?  
  23.  
  24. Of course there is always the possibility of a compiler bug (but it is
  25. unlikely). (Almost) the only way in C to get invalid memory accesses is
  26. through non-portable pointer operations (e.g. casting to a type with
  27. stricter alignment requirements).  In the case of:
  28.  
  29. >: >        typedef struct {
  30. >: >                ...
  31. >: >                unsigned short myshort;
  32. >: >                ...
  33. >: >                } RecType;
  34. >: >
  35. >: >        RecType *MyRec;
  36. >: >
  37. >: >Now when I try to dereference that member, casting it to (unsigned int),
  38. >: >I get a bus error:
  39. >: >
  40. >: >        unsigned int myint;
  41. >: >
  42. >: >        myint = (unsigned int) MyRec->myshort;  /* Bus error! */
  43.  
  44. the normal sequence of operations is to read an unsigned short from
  45. MyRec->myshort, convert the value to unsigned int and write it to myint.
  46. The only way that can fail is if MyRec isn't pointing to a valid RecType
  47. object (either not a valid address or not correctly aligned).
  48.  
  49. >: >I'm using GCC 2.7.2 on a SparcStation running SunOS 4.1.4. I've made sure
  50. >: >that the pointer points to valid data.
  51.  
  52. This is something you need to recheck. Print out the pointer value and see if 
  53. it is on an even address (assuming short is 2 'bytes' long).
  54.  
  55. -- 
  56. -----------------------------------------
  57. Lawrence Kirby | fred@genesis.demon.co.uk
  58. Wilts, England | 70734.126@compuserve.com
  59. -----------------------------------------
  60.